<template>
	<view class="content">
		<view class="head">
			<view class="title">
				<text>个人中心</text>
			</view>
			<view class="userinfo align-center" @click="toSet">
				<image class="avatar" :src=" userInfo.avatar || '/static/my/avatar.png'">
				</image>
				<view class="user-text flex-col justify-between">
					<view class="username">{{userInfo.nickname}}</view>
				</view>
			</view>
		</view>
		<view class="my-list">
			<u-cell-group :border="false">
				<u-cell title="我的农事" size="large" icon="/static/my/husbandry.png" :isLink="true" @click="toHusbandry">
				</u-cell>
				<u-cell title="我的地块" size="large" icon="/static/my/plant.png" :isLink="true" @click="toPlant"></u-cell>
				<u-cell title="我的咨询" size="large" icon="/static/my/consultation.png" :isLink="true"
					@click="toConsultation">
					<view slot="value">
						<u-badge type="error" max="99" :value="badgeInfo.value"></u-badge>
					</view>
				</u-cell>
				<u-cell title="个人信息" size="large" icon="/static/my/selfMsg.png" :isLink="true" @click="toSet"></u-cell>
			</u-cell-group>
		</view>
		<view class="op-btn" @click="toLogout">退出登录</view>
	</view>
</template>
<script>
import {loginOut} from "@/api/system/auth.js";
import {getConsultationList} from '@/api/my/consultation.js'

export default {
		data() {
			return {
				userInfo: {},
				badgeInfo: {
					value: 0
				},
				inter: null
			};
		},
		computed: {
			curUserInfo() {
				return this.$store.state.user.userInfo
			},
		},
		watch: {
			curUserInfo() {
				this.userInfo = this.curUserInfo
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		onShow() {
			this.getConsultationListFn()
		},
		onHide() {
			clearInterval(this.inter);
			this.inter = null;
		},
		methods: {
			// 10秒请求一次 看是否有新讯息
			getConsultationListFn() {
				let fn = async () => {
					let res = await getConsultationList({
						pageNo: 1,
						pageSize: 100,
						userId: this.userInfo.id,
						whetherView: 1
					});
					this.badgeInfo.value = res.list.length
				}
				fn()
				this.inter = setInterval(fn, 10000)
			},
			// 跳转修改个人信息 
			toSet() {
				uni.navigateTo({
					url: '/subPages/set/userInfo'
				})
			},
			// 跳转我的土地
			toPlant() {
				uni.navigateTo({
					url: '/subPages/my/plant/index'
				});
			},
			// 跳转我的农事
			toHusbandry() {
				uni.navigateTo({
					url: '/subPages/my/husbandry/index'
				});
			},
			// 跳转我的咨询
			toConsultation() {
				uni.navigateTo({
					url: '/subPages/my/consultation/index'
				});
			},
			// 退出登录
			toLogout() {
				let that = this;
				that.$util.model('您确定要退出登陆吗？').then(res => {
					if (res[1].confirm) {
						loginOut().then(res1 => {
							if (res1) {
								that.$util.msg('退出成功');
								that.$store.commit('logout')
							} else {
								that.$util.msg('退出失败,请重试');
							}
						});
					}
				}).catch((err) => {})
			}
		}
	};
</script>
<style lang='scss'>
	@import '../../common/css/common.css';
</style>
<style lang="scss" scoped>
	.content {

		.head {
			padding: 0 2rem 0 2.5rem;
			height: 26.75rem;
			width: 100%;
			background: linear-gradient(180deg, #51926D 0%, #74AA61 100%);

			.title {
				text-align: center;
				color: #FFFFFF;
				font-size: 1.8rem;
				font-weight: bold;
				padding-top: 5.4rem;
			}

			.userinfo {
				margin-top: 2.3rem;

				image {
					width: 5.5rem;
					height: 5.5rem;
				}

				.avatar {
					border-radius: 50%;
				}

				.user-text {
					margin-left: 1.3rem;
					height: 4.7rem;
					line-height: 4.7rem;

					.username {
						font-size: 1.6rem;
						font-weight: bold;
						color: #FFFFFF;
					}
				}
			}
		}

		.my-list {
			padding: 3rem;
			margin-top: -8.5rem;
			background: #fff;
			border-radius: 4rem 4rem 0 0;

			/deep/ .u-cell__body {
				padding: .75rem .5rem;
			}
		}

		.op-btn {
			position: absolute;
			bottom: 2.5rem;
			width: calc(100% - 4.4rem);
			margin: 2.2rem;
			height: 4rem;
			line-height: 4rem;
			text-align: center;
			background: rgba(153, 153, 153, 0.1);
			color: #999999;
			border-radius: .6rem;
		}
	}
</style>
